package com.xzuf.core.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Enumeration;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.JXStatusBar;

import com.xzuf.pub.GlobalValue;

/**
 * NC常用工具
 * @author xzgf
 * @since 2008-7-11
 * @version 1.0
 */
public class MainUI extends JFrame {

	private static final Log logger = LogFactory.getLog(MainUI.class);
	
	private static final String FrameTitle = "可应用扩展平台";
	
	private static final Dimension FrameSize = new Dimension(1280,768);
	
	private static final Font MENU_FONT = new Font("宋体", 0, 12);
    private static final Font DIALOG_FONT = new Font("dialog", 0, 12);
    private static final Font DEFAULT_FONT = new Font("dialog", 0, 12);
    
    private JSplitPane splitPane = new JSplitPane();
    
    private JDesktopPane pluginWindow = null;
    
    public MainUI() {
    	setUIStyle();
    	setUI();
    	GlobalValue.mainFrame = this;
    }
    
    /**
     * 
     * @author xzgf 2008-7-11
     */
    @SuppressWarnings("unchecked")
	private void setUIStyle() {
    	logger.debug("set custom UI style...");
    	setDefaultLookAndFeelDecorated(true);
    	Font stFont = getMenuFont();
        FontUIResource fontRes = new FontUIResource(stFont);
        for(Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements();)
        {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if(value instanceof FontUIResource)
                UIManager.put(key, fontRes);
        }

        UIManager.put("Menu.font", new FontUIResource(stFont));
        UIManager.put("TableHeader.font", new FontUIResource(stFont));
        UIManager.put("Tree.font", new FontUIResource(stFont));
        UIManager.put("TextArea.font", new FontUIResource(stFont));
        UIManager.put("EditorPane.font", new FontUIResource(stFont));
        UIManager.put("Table.font", new FontUIResource(stFont));
        logger.debug("custom UI style set complete...");
    }
    
    private void setMenu() {
    	MenuUI menuUI = new MenuUI();
    	setJMenuBar(menuUI.iniMenu());
    }
    
    private void setToolBar() {
    	ToolBarUI toolBarUI = new ToolBarUI();
    	add(toolBarUI.iniToolBar(),BorderLayout.NORTH);
    }
    
    /**
     * 
     * @author xzgf 2008-7-11
     */
    private void setUI() {
    	setTitle(FrameTitle);
    	setSize(FrameSize);
    	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	setLocationRelativeTo(null);
    	setLayout(new BorderLayout());
    	
    	setMenu();
    	setToolBar();
    	setStateBar();
    	
    	add(initContentPanel(),BorderLayout.CENTER);
    }
    
    
    private void setStateBar() {
    	JXStatusBar bar = new JXStatusBar();
    	JLabel statusLabel = new JLabel("Ready");
    	GlobalValue.stateLabel = statusLabel;
//    	bar.putClientProperty(BasicStatusBarUI.AUTO_ADD_SEPARATOR, false);
    	JProgressBar pbar = new JProgressBar(0,100);
    	GlobalValue.progressBar = pbar;
    	bar.add(statusLabel,JXStatusBar.Constraint.ResizeBehavior.FILL);
    	bar.add(new JSeparator(JSeparator.VERTICAL));
    	bar.add(pbar);
    	add(bar,BorderLayout.SOUTH);
    }
    
    private JPanel initContentPanel() {
    	JPanel mainPanel = new JPanel();
    	
    	splitPane.setDividerLocation(189);
    	splitPane.setDividerSize(8);
    	splitPane.setOneTouchExpandable(true);
    	splitPane.add(initLeftPluginTree(),JSplitPane.LEFT);
    	pluginWindow = new JDesktopPane();
    	splitPane.add(pluginWindow,JSplitPane.RIGHT);
    	GlobalValue.splitPane = splitPane;
    	GlobalValue.pluginWindows = pluginWindow;
    	mainPanel.setLayout(new BorderLayout());
    	mainPanel.add(splitPane,BorderLayout.CENTER);
    	return mainPanel;
    }
    
    private JPanel initLeftPluginTree() {
    	JPanel lfTreePanel = new JPanel();
    	lfTreePanel.setLayout(new BorderLayout());
//    	PluginTreeUI treeUI = new PluginTreeUI();
    	PluginTaskPane taskPane = new PluginTaskPane();
//    	PluginControlUI controlUI = new PluginControlUI();
//    	lfTreePanel.add(controlUI.initPluginControl(),BorderLayout.NORTH);
    	lfTreePanel.add(taskPane,BorderLayout.CENTER);
    	GlobalValue.pluginTreePanel = lfTreePanel;
    	return lfTreePanel;
    }
    
    public Font getDialogFont() {
    	if(DIALOG_FONT != null) {
    		return DIALOG_FONT;
    	}
    	return DEFAULT_FONT;
    }
    
    public Font getMenuFont() {
    	if(MENU_FONT != null) {
    		return MENU_FONT;
    	}
    	return DEFAULT_FONT;
    }
}
